package tongwentongshu.com.app.presenter;

import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import java.util.Map;

import tongwentongshu.com.app.bean.AppItem;
import tongwentongshu.com.app.contract.BackPasswordContract;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;
import tongwentongshu.com.app.utils.RegularUtils;

/**
 * Created by XiongBin on 2017/7/13.
 */

public class BackPasswordPresenter implements BackPasswordContract.Presenter {
    BackPasswordContract.View backPasswordView;
    private Map<String, String> map;

    public BackPasswordPresenter(BackPasswordContract.View backPasswordView) {
        this.backPasswordView = backPasswordView;
    }

    @Override
    public void sendCode() {
        String phone = backPasswordView.getPhone();
        map = new ArrayMap<>();
        map.put("mobile", phone);
        UpdateFractory.getBuild()
                .name("SendCodCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem poiBean, String msg) {
                        backPasswordView.onSuccess(msg, false);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }

    public void baseVerification() {
        String phone = backPasswordView.getPhone();
        if (TextUtils.isEmpty(phone)) {
            backPasswordView.onShowError("手机号不能为空");
            return;
        }
        if (!RegularUtils.isMobileExact(phone)) {
            backPasswordView.onShowError("请输入正确的手机号");
            return;
        }
    }

    @Override
    public void resetPwd() {
        baseVerification();
        String phone = backPasswordView.getPhone();
        String code = backPasswordView.getCode();
        String pw = backPasswordView.getPw();
        String rpw = backPasswordView.getRpw();
        if (TextUtils.isEmpty(code)) {
            backPasswordView.onShowError("请输入验证码");
            return;
        }
        if (TextUtils.isEmpty(pw)) {
            backPasswordView.onShowError("请设置密码");
            return;
        }
        if (TextUtils.isEmpty(rpw)) {
            backPasswordView.onShowError("请再次输入密码");
            return;
        }
        if (!rpw.equals(pw)) {
            backPasswordView.onShowError("两次密码不一致");
            return;
        }
        map = new ArrayMap<>();
        map.put("password", pw);
        map.put("repassword", rpw);
        map.put("code", code);
        map.put("mobile", phone);
        UpdateFractory.getBuild()
                .name("ResetPwdCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<AppItem>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(AppItem poiBean, String msg) {
                        backPasswordView.onSuccess(msg, true);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                    }
                });
    }
}
